import {h, init} from "snabbdom"

function defineReactive(obj, key, val) {
    Object.defineProperty(obj, key, {
        get() {
            console.log('get ' + val);
            return val;
        },
        set(v) {
            if(v !== val) {
                val = v;
                console.log('set' + v);
                update()
            }
        }
    })
}
const obj = {}
let vnode
let app = document.querySelector('#app')

//1.获取patch函数，对比两个虚拟dom，执行dom操作
const patch = init([])

function update() {
    vnode = patch(vnode, h('div#app', obj.foo))
}

defineReactive(obj, 'foo', new Date().toLocaleTimeString())
//执行初始化
vnode = patch(app, h('div#app', obj.foo))
console.log(vnode)


setInterval(() => {
    obj.foo = new Date().toLocaleTimeString()
}, 1000)